package com.itcast.monkey.web.action;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import com.itcast.monkey.domain.base.Standard;
import com.itcast.monkey.service.base.StandardService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @author			
 * @project_name	monkey_bos_management
 * @data   			2018年3月7日下午2:07:21 
 */

@Controller
@Scope("prototype")
@Namespace("/")
@ParentPackage("json-default")
public class StandardAction extends ActionSupport implements ModelDriven<Standard>{
	
	private Standard standard = new Standard();
	
	@Override
	public Standard getModel() {
		return standard;
	}
	
	private int page;
	
	private int rows;

	public void setPage(int page) {
		this.page = page;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	@Autowired
	private StandardService standardService;
	
	@Action(value="standard_save",
			results={@Result(name="success",location="./pages/base/standard.html",type="redirect")})
	public String Save(){
		standardService.save(standard);
		return SUCCESS;
	}
	
	@Action(value="standard_pageQuery",results={@Result(name="success",type="json")})
	public String pageQuery(){
		Pageable pageable = new PageRequest(page-1, rows);
		Page<Standard> pageQuery = standardService.pageQuery(pageable);
		Map<String , Object> reslut = new HashMap<>();
		reslut.put("total", pageQuery.getTotalElements());
		reslut.put("rows", pageQuery.getContent());
		ActionContext.getContext().getValueStack().push(reslut);
		return SUCCESS;
	}
	
	@Action(value="standard_findAll",results={@Result(name="success",type="json")})
	public String findAll(){
		List<Standard> list = standardService.findAll();
		ActionContext.getContext().getValueStack().push(list);
		return SUCCESS;
	}
	
}
